﻿@{
	Layout = null;
}
<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" action="" style="margin-top: 20px;margin-left: 30px">
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">工作站编号</label>
			<div class="layui-input-inline layui-input-wrap">
				<input type="text" name="workstationCode" id="workstationCode" lay-verify="required" placeholder="请输入" class="layui-input">
			</div>
			<div class="layui-form-mid">
				<input type="checkbox" lay-skin="switch" lay-filter="switchTest">自动生成
			</div>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">工作站名称</label>
		<div class="layui-input-block">
			<input type="text" name="workstationName" lay-verify="required" style="width:350px" placeholder="请输入" class="layui-input">
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">工作站地点</label>
		<div class="layui-input-block">
			<input type="text" name="workstationPlace" placeholder="请输入" lay-verify="required" style="width:350px" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">所在车间</label>
		<div class="layui-input-block" style="width:350px">
			<select name="workshopName" style="width:350px" lay-filter="aihao">
				<option value="五金车间" selected>五金车间</option>
				<option value="组装车间">组装车间</option>
				<option value="注塑车间">注塑车间</option>
				<option value="二楼插丝车间">二楼插丝车间</option>
				<option value="二楼焊接车间">二楼焊接车间</option>
			</select>
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">所属工序</label>
		<div class="layui-input-block" style="width:350px">
			<select name="processBelongs" style="width:350px" lay-filter="aihao">
				<option value="板材剪贴" selected>板材剪贴</option>
				<option value="组装">组装</option>
				<option value="QCC质检">QCC质检</option>
				<option value="包装">包装</option>
				<option value="尼龙棒">尼龙棒</option>
			</select>
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">是否启用</label>
		<div class="layui-input-block">
			<input type="radio" name="state" value="1" title="启用" checked>
			<input type="radio" name="state" value="0" title="禁用">
		</div>
	</div>

	<div class="layui-form-item">
		<div class="layui-input-block">
			<button type="submit" class="layui-btn" lay-submit lay-filter="addWorkstation" lay-on="alert">保存</button>
			<button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkstation()">关闭</button>
		</div>
	</div>
</form>
<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>

<script>

	layui.use(['form', 'laydate', 'util'], function () {
	var form=layui.form;
	var layer=layui.layer;
	var laydate=layui.laydate;
	var util=layui.util;
	//指定开关事件，生成工作站编码
	form.on('switch(switchTest)',function(data){
		if(data.elem.checked){
			var random =Math.floor(Math.random()*10)+'';
			for(var i=0;i<3;i++){
				random+=Math.floor(Math.random()*10);
			}
			random='WS'+random;
			$('#workstationCode').val(random);
		}else{
			$('#workstationCode').val("");
		}
	});
	});

	//关闭弹框
	function closeWorkstation() {
		var index = parent.layer.getFrameIndex(window.name);
		window.parent.location.reload(); // 刷新父页
		parent.layer.close(index); //再执行关闭
	}
	//工作站编号
	// var number = "WS" + Math.floor(1000 + Math.random() * 9000);
	// $("#workstationCode").val(number)
	layui.use(['form', 'laydate', 'util','layer'], function () {
		var form = layui.form;
		var layer = layui.layer;
		var laydate = layui.laydate;
		var util = layui.util;

		// 添加事件
		form.on('submit(addWorkstation)', function (data) {
			var field = data.field; // 获取表单字段值
			// 此处可执行 Ajax 等操作
			$.ajax({
				type: 'post',
				url: '/Workstation/AddWorkstation',
				data: field,
				dataType: 'json',
				success: function (res) {
					console.log(1111,res);
					if(res==-1)
					{
						layer.msg('工作站编号已存在！请重新输入！', {
							time: 1000,
							icon: 5
						});
					}
				   else if(res ==-2)
					{
						layer.msg('工作站名称已存在！请重新输入！', {
							time: 1000,
							icon: 5
						});
					}
					else if(res ==0){
						layer.alert('添加成功', { title: '操作结果' }, function () {
						// 添加成功后的逻辑，例如关闭弹窗，刷新表格等
						var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
						window.parent.location.reload(); // 刷新父页
						parent.layer.close(index); //再执行关闭
						setTimeout(function () { parent.layer.close(index) }, 50000);//延迟s
						});
					}

			}
			})
			return false; // 阻止默认 form 跳转
		});
		});


</script>
